﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;


namespace simWaifu
{
    public partial class FormChatSettings : Form
    {
        public FormChatSettings()
        {
            InitializeComponent();
        }

        private string configFileName = "";
        
        public FormChatSettings(string fileName)
        {
            InitializeComponent();
            configFileName = fileName;
            LoadSettingsFile();
            UnlockForm();
        }

        private void LockForm()
        {
            // lock all elements (while loading/saving stuff)
            listBoxName.Enabled = false;
            textBoxName.Enabled = false;
            listBoxValue.Enabled = false;
            textBoxValue.Enabled = false;
            listBoxNameInternal.Enabled = false;
            textBoxNameInternal.Enabled = false;
            listBoxValueInternal.Enabled = false;
            textBoxValueInternal.Enabled = false;
        }

        private void UnlockForm()
        {
            // unlock elements depending on dummy/expert mode
            if (expertModeToolStripMenuItem.Checked)
            {
                listBoxName.Enabled = true;
                textBoxName.Enabled = true;
                listBoxValue.Enabled = true;
                textBoxValue.Enabled = true;
                listBoxNameInternal.Enabled = true;
                textBoxNameInternal.Enabled = true;
                listBoxValueInternal.Enabled = true;
                textBoxValueInternal.Enabled = true;
            }
            else
            {
                listBoxName.Enabled = true;
                textBoxName.Enabled = false;
                listBoxValue.Enabled = true;
                textBoxValue.Enabled = true;
                listBoxNameInternal.Enabled = true;
                textBoxNameInternal.Enabled = false;
                listBoxValueInternal.Enabled = true;
                textBoxValueInternal.Enabled = false;
            }
        }

        private void LoadSettingsFile()
        {
            LockForm();

            listBoxName.Items.Clear();
            listBoxValue.Items.Clear();
            listBoxNameInternal.Items.Clear();
            listBoxValueInternal.Items.Clear();

            SettingsHandler.LoadAIMLConfigFile(configFileName);

            for (int i = 0; i < SettingsHandler.internalBotConstantNames.Count; i++)
            {
                listBoxNameInternal.Items.Add( SettingsHandler.internalBotConstantNames[i] );
                listBoxValueInternal.Items.Add( SettingsHandler.internalBotConstantValues[i] );
            }

            for (int e = 0; e < SettingsHandler.editableBotConstantNames.Count; e++)
            {
                listBoxName.Items.Add(SettingsHandler.editableBotConstantNames[e]);
                listBoxValue.Items.Add(SettingsHandler.editableBotConstantValues[e]);
            }

            UnlockForm();
        }

        private void SaveSettingsFile()
        {
            LockForm();

            SettingsHandler.internalBotConstantNames.Clear();
            SettingsHandler.internalBotConstantValues.Clear();
            SettingsHandler.editableBotConstantNames.Clear();
            SettingsHandler.editableBotConstantValues.Clear();

            for (int i = 0; i < listBoxNameInternal.Items.Count; i++ )
            {
                SettingsHandler.internalBotConstantNames.Add(listBoxNameInternal.Items[i].ToString());
                SettingsHandler.internalBotConstantValues.Add(listBoxValueInternal.Items[i].ToString());
            }
            for (int e = 0; e < listBoxName.Items.Count; e++ )
            {
                SettingsHandler.editableBotConstantNames.Add(listBoxName.Items[e].ToString());
                SettingsHandler.editableBotConstantValues.Add(listBoxValue.Items[e].ToString());
            }
            
            SettingsHandler.SaveAIMLConfigFile();
            
            UnlockForm();
        }

        private void listBoxName_SelectedIndexChanged(object sender, EventArgs e)
        {
            int i = listBoxName.SelectedIndex;
            if (i >= 0)
            {
                listBoxValue.SelectedIndex = i;
                textBoxName.Text = listBoxName.Items[i].ToString();
            }
        }

        private void listBoxValue_SelectedIndexChanged(object sender, EventArgs e)
        {
            int i = listBoxValue.SelectedIndex;
            if (i >= 0)
            {
                listBoxName.SelectedIndex = i;
                textBoxValue.Text = listBoxValue.Items[i].ToString();
            }
        }

        private void textBoxName_TextChanged(object sender, EventArgs e)
        {
            int i = listBoxName.SelectedIndex;
            listBoxName.Items.RemoveAt(i);
            listBoxName.Items.Insert(i, textBoxName.Text);
            listBoxName.SelectedIndex = i;
        }

        private void textBoxValue_TextChanged(object sender, EventArgs e)
        {
            int i = listBoxValue.SelectedIndex;
            listBoxValue.Items.RemoveAt(i);
            listBoxValue.Items.Insert(i, textBoxValue.Text);
            listBoxValue.SelectedIndex = i;
        }

        private void FormChatSettings_Load(object sender, EventArgs e)
        {
        }

        private void FormChatSettings_Closing(object sender, FormClosingEventArgs e)
        {
            SaveSettingsFile();
        }

        private void expertModeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            expertModeToolStripMenuItem.Checked = !expertModeToolStripMenuItem.Checked;
            UnlockForm();
        }

        private void listBoxNameInternal_SelectedIndexChanged(object sender, EventArgs e)
        {
            int i = listBoxNameInternal.SelectedIndex;
            if (i >= 0)
            {
                listBoxValueInternal.SelectedIndex = i;
                textBoxNameInternal.Text = listBoxNameInternal.Items[i].ToString();
            }
        }

        private void textBoxNameInternal_TextChanged(object sender, EventArgs e)
        {
            int i = listBoxNameInternal.SelectedIndex;
            listBoxNameInternal.Items.RemoveAt(i);
            listBoxNameInternal.Items.Insert(i, textBoxNameInternal.Text);
            listBoxNameInternal.SelectedIndex = i;
        }

        private void listBoxValueInternal_SelectedIndexChanged(object sender, EventArgs e)
        {
            int i = listBoxValueInternal.SelectedIndex;
            if (i >= 0)
            {
                listBoxNameInternal.SelectedIndex = i;
                textBoxValueInternal.Text = listBoxValueInternal.Items[i].ToString();
            }
        }

        private void textBoxValueInternal_TextChanged(object sender, EventArgs e)
        {
            int i = listBoxValueInternal.SelectedIndex;
            listBoxValueInternal.Items.RemoveAt(i);
            listBoxValueInternal.Items.Insert(i, textBoxValueInternal.Text);
            listBoxValueInternal.SelectedIndex = i;
        }

        private void reloadToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.LoadSettingsFile();
        }

        private void saveToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.SaveSettingsFile();
        }
    }
}
